//
//  PPShowViewController.m
//  FileView-Simple
//
//  Created by 甘文鹏 on 2018/4/24.
//  Copyright © 2018年 ganwenpeng.com. All rights reserved.
//

#import "PPShowViewController.h"
#import "FileView.h"
#import "UIColor+PP.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import <MJExtension/MJExtension.h>
#import "PPFileInfo.h"

@interface PPShowViewController ()
@property (weak, nonatomic) IBOutlet FileView *fileView;
@property (weak, nonatomic) IBOutlet UISlider *distanceOfBottomPointToRectangleRightSlider;
@property (weak, nonatomic) IBOutlet UISlider *lengthOfLeftForTriangleSlider;
@property (weak, nonatomic) IBOutlet UISlider *angleOfBrokenLineToRectangleBottomSlider;
@property (weak, nonatomic) IBOutlet UISlider *angleOfBrokenLineToTriangleLeftSlider;

@property (nonatomic, assign) CGFloat half;
@property (nonatomic, strong) NSArray<PPFileInfo *> *files;
@end

@implementation PPShowViewController

- (NSArray<PPFileInfo *> *)files {
    if (!_files) {
        _files = [PPFileInfo mj_objectArrayWithFilename:@"file.plist"];
    }
    return _files;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupSlider];
    
    // 设置折线与矩形底边的夹角
    self.fileView.angleOfBrokenLineToRectangleBottom = self.angleOfBrokenLineToRectangleBottomSlider.value;
    // 设置折线与折角左侧边（以折线为底边）的夹角
    self.fileView.angleOfBrokenLineToTriangleLeft = self.angleOfBrokenLineToTriangleLeftSlider.value;
    // 设置矩形底边上折点到矩形右侧边的距离
    self.fileView.distanceOfBottomPointToRectangleRight = self.distanceOfBottomPointToRectangleRightSlider.value;
    // 设置折角左侧边（以折线为底边）的长度
    self.fileView.lengthOfLeftForTriangle = self.lengthOfLeftForTriangleSlider.value;
    // 设置内外背景色
    self.fileView.outerBgColor = [UIColor createWithRed:230 green:80 blue:85 alpha:1];
    self.fileView.innerBgColor = [UIColor createWithRed:249 green:209 blue:210 alpha:1];
    // 设置内外背景色构成的边框的大小
    self.fileView.borderWidth = 1;
    
    // 设置文件名相关属性
    self.fileView.fileNameTextColor = [UIColor blackColor];
    self.fileView.fileNameFont = [UIFont systemFontOfSize:15];
    self.fileView.maxLinesOfFileName = 0;
    
    // 设置文件大小相关属性
    self.fileView.fileSizeTextColor = [UIColor grayColor];
    self.fileView.fileSizeFont = [UIFont systemFontOfSize:12];
    
    [self switchFileClick];
    // 打开注释，可以设置label的四边间距
//    self.fileView.contentInsets = UIEdgeInsetsMake(5, 10, 15, 20);
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.half = MIN(self.fileView.frame.size.width, self.fileView.frame.size.height) / 2;
    [self.angleOfBrokenLineToTriangleLeftSlider sendActionsForControlEvents:UIControlEventValueChanged];
    [self.angleOfBrokenLineToRectangleBottomSlider sendActionsForControlEvents:UIControlEventValueChanged];
    [self.distanceOfBottomPointToRectangleRightSlider sendActionsForControlEvents:UIControlEventValueChanged];
    [self.lengthOfLeftForTriangleSlider sendActionsForControlEvents:UIControlEventValueChanged];
}

- (void)setupSlider {
    __weak typeof(self) weakSelf = self;
    
    self.distanceOfBottomPointToRectangleRightSlider.value = 0.5;
    [[self.distanceOfBottomPointToRectangleRightSlider rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.fileView.distanceOfBottomPointToRectangleRight = weakSelf.distanceOfBottomPointToRectangleRightSlider.value * weakSelf.half;
    }];
    
    self.lengthOfLeftForTriangleSlider.value = 0.5;
    [[self.lengthOfLeftForTriangleSlider rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.fileView.lengthOfLeftForTriangle = weakSelf.lengthOfLeftForTriangleSlider.value * weakSelf.half;
    }];
    
    self.angleOfBrokenLineToRectangleBottomSlider.minimumValue = 0;
    self.angleOfBrokenLineToRectangleBottomSlider.maximumValue = 90;
    self.angleOfBrokenLineToRectangleBottomSlider.value = 45;
    [[self.angleOfBrokenLineToRectangleBottomSlider rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.fileView.angleOfBrokenLineToRectangleBottom = weakSelf.angleOfBrokenLineToRectangleBottomSlider.value;
    }];
    
    self.angleOfBrokenLineToTriangleLeftSlider.minimumValue = 0;
    self.angleOfBrokenLineToTriangleLeftSlider.maximumValue = 90;
    self.angleOfBrokenLineToTriangleLeftSlider.value = 60;
    [[self.angleOfBrokenLineToTriangleLeftSlider rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.fileView.angleOfBrokenLineToTriangleLeft = weakSelf.angleOfBrokenLineToTriangleLeftSlider.value;
    }];
}

- (IBAction)switchFileClick {
    PPFileInfo *file = [self.files objectAtIndex:(arc4random() % self.files.count)];
    // 去重
    if ([file.fileName isEqualToString:self.fileView.fileNameText]) {
        [self switchFileClick];
        return;
    }
    self.fileView.fileNameText = file.fileName;
    self.fileView.fileSizeText = file.fileSizeText;
    self.fileView.maxLinesOfFileName = arc4random() % self.files.count;
}
@end
